package com.example.happiness.controller.employee;

import com.example.happiness.constant.JwtClaimsConstant;
import com.example.happiness.pojo.Admin;
import com.example.happiness.pojo.Employee;
import com.example.happiness.pojo.adminvo.AdminLoginVO;
import com.example.happiness.pojo.employeevo.EmployRevenueVO;
import com.example.happiness.pojo.employeevo.EmployeeLoginVO;
import com.example.happiness.pojo.employeevo.EmployeeRecordVO;
import com.example.happiness.properties.JwtProperties;
import com.example.happiness.result.Result;
import com.example.happiness.service.employee.EmployeeOrderService;
import com.example.happiness.service.employee.EmployeeService;
import com.example.happiness.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "员工相关接口")
@Slf4j
@RestController
@RequestMapping("/employee/employee")
public class EmployController {


    @Autowired
    EmployeeService employeeService;
    @Autowired
    JwtProperties jwtProperties;
    @Autowired
    EmployeeOrderService employeeOrderService;


    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<EmployeeLoginVO> userlogin(@RequestBody Employee employee){
        Employee employeevo = employeeService.emplayeeLogin(employee);
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        log.info("管理员登录，工号：{}，密码：{}",employee.getEmployeeid(),password);
        if (employeevo == null) {
            return Result.error("没有相关员工");
        }else if(!employeevo.getPassword().equals(password)){
            return Result.error("密码错误");
        }
        Map<String,Object> claims = new HashMap();
        claims.put(JwtClaimsConstant.USER_ID,employeevo.getEmployeeid());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        System.out.println(jwtProperties.getUserSecretKey());
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .employee(employeevo)
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    @GetMapping("/getRevenue")
    public Result<EmployRevenueVO> getRevenue(String eid){
        EmployRevenueVO recordVO = employeeOrderService.getRevenue(eid);
        return Result.success(recordVO);
    }
}
